package net.yunzhanyi.client.config;

import net.yunzhanyi.client.interceptor.AjaxLoginInterceptor;
import net.yunzhanyi.client.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver;
import org.springframework.mobile.device.DeviceResolverHandlerInterceptor;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author bestct
 * @date 2022/10/1
 * @type 类
 */
@Configuration
public class WebConfigurer implements WebMvcConfigurer {



    @Bean
    LoginInterceptor pageLoginInterceptor() {
        return new LoginInterceptor();
    }

    @Bean
    AjaxLoginInterceptor ajaxLoginInterceptor() {
        return new AjaxLoginInterceptor();
    }

    /**
     * 设备解析处理
     * @return
     */
    @Bean
    public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() {
        return new DeviceResolverHandlerInterceptor();
    }

    /**
     * 请求中Device参数的处理
     * @return
     */
    @Bean
    public DeviceHandlerMethodArgumentResolver deviceHandlerMethodArgumentResolver() {
        return new DeviceHandlerMethodArgumentResolver();
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(deviceHandlerMethodArgumentResolver());
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] strs = {"/css/**","/js/**","/api/**","/img/**","/error/**","/login","/404"};
        //注册登录拦截器
        registry
                .addInterceptor(pageLoginInterceptor())
                //所有路径都被拦截
                .addPathPatterns("/**")
                .excludePathPatterns(strs);
        // 静态资源请求不拦截请附上原文出处链接及本声明。
        registry.addInterceptor(ajaxLoginInterceptor())
                .addPathPatterns("/api/**")
                .excludePathPatterns("/api/login","/api/register");
        //添加设备拦截
        registry.addInterceptor(deviceResolverHandlerInterceptor())
                .addPathPatterns("/api/login/**");
    }

}
